package com.health.controller;

import com.health.pojo.Users;
import com.health.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

// 个人信息修改查询信息Servlet
@WebServlet("/sendInformation")
public class SendInformationServlet extends HttpServlet {
    UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Integer userId = ((Users) request.getSession().getAttribute("user")).getUserId();

        String userName = new String(request.getParameter("nameText").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        String userRealName = new String(request.getParameter("realNameText").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        String userPassword = new String(request.getParameter("passwordText").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        String userGender = new String(request.getParameter("selectGender").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);


        Users user = new Users(userId, userName, userRealName, userGender, userPassword, "user-img/wwj.jpg", null, null);
        if (userService.sendInformation(user)) {
            request.getSession().removeAttribute("user");
            request.getSession().setAttribute("user", user);
            request.setAttribute("updateStatus", true);
        }
//        response.sendRedirect(request.getContextPath()+"/editInformation");
        request.getRequestDispatcher("restrictedPages/main.jsp").forward(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
